package com.qfedu.agro.vo;

import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.qfedu.agro.pojo.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Name: LoginUser
 * Package: com.qfedu.agro.vo
 * Date: 2024/10/24 - 11:13
 * Description:
 *
 * @author Junhui Zhang
 * @version v1.0
 */

@Data
@JsonIncludeProperties(value = {"sysUser", "permissions"})
public class LoginUser implements UserDetails {

    private SysUser sysUser;

    private List<String> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        if (permissions == null) {
            return null;
        }
        permissions.forEach(permission -> grantedAuthorities.add(new SimpleGrantedAuthority(permission)));
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
